var common = require('../../../utils/mr_xu/common.js')
var orderCache = require('../../../utils/mr_xu/apply_order_cache.js')
var api = require("../../../utils/mr_xu/api_url.js").api
var request = require("../../../utils/mr_xu/request.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode_show: true,
    price: '',
    limitPrice: '',
    limitTime: '',
    limitTimeShow: '',
    startTime: '',
    endTime: '',
    minPrice: 50,
    minTime: 15,
    revokeMoney: 0,
    compareDistance: 0,
    compareMoney: 0
  },
  input_price: function(res) {
    this.setData({
      price: res.detail.value
    })
  },
  input_limit_price: function(res) {
    this.setData({
      limitPrice: res.detail.value
    })
  },
  bindTimeChange(res) {
    console.log(res)
    this.setData({
      limitTime: res.detail.value + " 23:59:59",
      limitTimeShow: res.detail.value,
    })
  },
  marketPirce() {
    return 800
  },
  next: function() {

    let that = this

    if (that.data.limitPrice && that.data.limitPrice < that.data.minPrice) {
      wx.showModal({
        title: '提示',
        content: '最低加价不能低于' + that.data.minPrice,
        showCancel:false
      })
      return
    }

    common.safeRun(function() {

      orderCache.setItem('price', that.data.price)
      orderCache.setItem('limitPrice', that.data.limitPrice)
      orderCache.setItem('limitTime', that.data.limitTime)

      let data = orderCache.getAll()
      
      //走修改的逻辑
      let updateInfo = orderCache.getUpdateInfo()
      console.log('xxxxxxxxxxxxx')
      console.log(updateInfo)
      if (updateInfo) {
        data.id = updateInfo.id
      }

      if (that.data.price < that.marketPirce()) {
        wx.showModal({
            title: '提示',
            content: '您的报价低于市场参考价，确定发布？',
            success: function(res) {
              console.log(res)
              if (res.confirm) {
                request.post(api.find_car_apply, {
                  data: JSON.stringify(data)
                }, function(res) {
                  wx.showToast({
                    title: res.msg,
                  })
                  if (res.code == 200) {
                    orderCache.clearCache()
                    orderCache.deleteUpdateInfo()
                    wx.redirectTo({
                      url: '/pages/user_side/order_info/order_info?toIndex=1&id=' + res.obj,
                    })
                  }
                })
              }
            },
            cancelText: "修改",
            confirmText: "依旧发布"
          })
          return
      }

      request.post(api.find_car_apply, {
        data: JSON.stringify(data)
      }, function(res) {
        console.log(res)
        wx.showToast({
          title: res.msg,
        })
        if (res.code == 200) {
          orderCache.clearCache()
          orderCache.deleteUpdateInfo()
          wx.redirectTo({
            url: '/pages/user_side/order_info/order_info?toIndex=1&id=' + res.obj,
          })
        }
      })
    })
  },
  onLoad() {

    let revokeMoney = common.getConfig('QUEST_REVOKE_MONEY')
    let compareDistance = common.getConfig('QUEST_DIFERENCE_DISTANCE')
    let compareMoney = common.getConfig('QUEST_DISTANCE_PRICE')

    this.setData({
      revokeMoney: revokeMoney,
      compareDistance: compareDistance,
      compareMoney: compareMoney
    })

    let that = this
    request.post(api.system_config, {
      "data": ""
    }, function(res) {
      let items = res.obj
      var minPrice = 50
      var minTime = 15
      if (items) {
        for (var i in items) {
          if (items[i].key == 'QUEST_LIMIT_MIN_PRICE') {
            minPrice = items[i].value
          }
          if (items[i].key == 'QUEST_LIMIT_MIN_TIME') {
            minTime = items[i].value
          }
        }
      }

      that.setData({
        minPrice: minPrice,
        minTime: minTime
      })

      that.setData({
        startTime: common.formatTime(new Date(), 0, 0, parseInt(minTime)),
        endTime: common.formatTime(new Date(), 5)
      })
    })


    let updateInfo = orderCache.getUpdateInfo()
    console.log(updateInfo)
    if (updateInfo) {
      
      that.setData({
        price: updateInfo.price,
        limitPrice: updateInfo.limitPrice,
        limitTime: updateInfo.limitTime,
        limitTimeShow: updateInfo.limitTime ? updateInfo.limitTime.substring(0,10) : '',
      })
    }

  },

})